package com.example.j2025_11_6;

import com.sun.source.tree.UsesTree;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.font.MultipleMaster;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class Test {

    @RequestMapping("/r1")
    public int r1(int key){
        return key;
    }

    @RequestMapping("r2")
    public String r2(@RequestBody Student student){
        return student.toString();
    }

    @RequestMapping("r3/{key}")
    public String r3(@PathVariable String key){
        return key;
    }

    @RequestMapping("r4")
    public String r3(@RequestPart("file") MultipartFile File) throws IOException {
       File.transferTo(new File("D:\\我的java代码\\java"+File.getOriginalFilename()));
       return "接收成功";
    }

    @RequestMapping("r5")
    public String r5(HttpServletRequest http){
        Cookie[] cookies = http.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies){
                cookie.getAttribute("Cookie name:"+ cookie.getName());
            }
        }
        return  "接收成功";
    }

    @RequestMapping("r6")
    public String r6(@CookieValue("name") String name){
        return "name = "+ name;
    }
    @RequestMapping("r7")
    public String r7(HttpServletRequest http){
       HttpSession session = http.getSession();
       session.setAttribute("name","lisi");
       session.setAttribute("age",18);
       return "设置成功";
    }
    @RequestMapping("r8")
    public String r8(HttpServletRequest http){
        HttpSession session = http.getSession();
        if (session == null){
            return null;
        }else {
            String value = (String) session.getAttribute("name");
            return "name = " + value;
        }
    }
    @RequestMapping("r9")
    public String r9(HttpSession session){
        if (session == null){
            return null;
        }else {
            String value = (String) session.getAttribute("name");
            return "name = " + value;
        }
    }
    @RequestMapping("r10")
    public String r10(@SessionAttribute("age") Integer age){
        return "age = "+age;
    }
    @RequestMapping("r11")
    public String r11(@RequestParam List<List<Integer>> list){
        return "List = "+list;
    }
    @RequestMapping("r12")
    public String r12(HttpServletRequest http){
         return "U-Agent = " + http.getHeader("User-Agent");
    }
    @RequestMapping("r13")
    public String r13(@RequestHeader("User-Agent") String userAgent){
        return "User-Agent ="+ userAgent;
    }
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return "从header中获取userAgent:"+ userAgent;
    }
}
